package com.example.microinvestmarket.Runnable;
import com.example.microinvestcommon.util.StockPriceUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author tiaanwailaiwu
 * 用于线程快速初始化Stock数据
 */

public class InitializeStockDataRunnable implements Runnable {
    private static final List<List<Double>> stockDataList = Collections.synchronizedList(new ArrayList<>());
    private final int totalTurnNum;
    private final int everyTurnNum;
    private final int maxNum;
    private final int minNum;

    public InitializeStockDataRunnable(int totalTurnNum, int everyTurnNum, int maxNum, int minNum) {
        this.totalTurnNum = totalTurnNum;
        this.everyTurnNum = everyTurnNum;
        this.maxNum = maxNum;
        this.minNum = minNum;
    }

    @Override
    public void run() {
        //保留两位小数
        double firstPrice = Math.round((minNum + Math.random() * (maxNum - minNum)) * 100.0) / 100.0;
        List<Double> newStockData = StockPriceUtil.createStockPrices(firstPrice, totalTurnNum, everyTurnNum);
        stockDataList.add(newStockData);
    }

    public static List<List<Double>> getStockDataList() {
        return new ArrayList<>(stockDataList); // 返回副本避免外部修改
    }

    public static void clearStockDataList() {
        stockDataList.clear();
    }
}
